package com.myacg.editor.common.unit.panel.base;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.myacg.editor.common.unit.button.CrystalButton;
import com.myacg.editor.common.utils.JFrameUtils;
/**
 * 使用套接的形式将绘制shadowSize部分为阴影， shadowSize以内以绝对位置的方式放置
 * @author Administrator
 *
 */
public class ShadowPanel extends JPanel{
	public static final int TYPE_INNER = 0;
	public static final int TYPE_OUT = 1;
	public static final int TYPE_CENTER = 2;
	private JComponent  component;
	private Color shadowColor;
	private int shadowSize;
	private int s;
	private int range;
	public ShadowPanel(JComponent  component,Color shadowColor,int shadowSize , int s){
		this.shadowSize = shadowSize;
		this.shadowColor = shadowColor;
		this.s =  s ; 
		this.range=0;
		this.component = component;
		this.setLayout(null);
		this.add(this.component);
		 
	}
	 @Override
	public void paint(Graphics g) {
		super.paint(g); 
			 //绘制 阴影区域
			this.drawShadowToPanel(g, this, shadowColor, shadowSize, s, range , false);
			//绘制子组件
			this.component.setBounds(shadowSize, shadowSize, getWidth() - 2*shadowSize + 1, getHeight() - 2*shadowSize +1);
	}
	 
	 /**
	  * 绘制对应的border
	  * @param g
	  * @param jpanel
	  * @param baseColor
	  * @param shadowSize
	  * @param range
	  */
	 public void drawBorder(Graphics g,JPanel jpanel ,Color baseColor,int shadowSize, int range){
		 int x =  shadowSize ;
		 int y =  shadowSize ;
		 g.setColor(baseColor); //设置颜色!! 
		 RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(x,y,jpanel.getWidth() -2*x, jpanel.getHeight() -2*y , range, range);
         ((Graphics2D) g).draw(roundedRectangle);
	 }
	 /**
	  * 绘制阴影部分
	  * @param g
	  * @param jpanel
	  * @param baseColor
	  * @param drawLength
	  * @param s
	  * @param range
	  * @param isInner
	  */
	 public   void drawShadowToPanel(Graphics g,JPanel jpanel ,Color baseColor,int drawLength,int s, int range , boolean isInner){
		  int width =  jpanel.getWidth();
		  int height =  jpanel.getHeight(); 
		  //jpanel控件所在的背景
		  Color parentBgColor = jpanel.getParent().getBackground();
		  int p_r = parentBgColor.getRed();
		  int p_g = parentBgColor.getGreen();
		  int p_b = parentBgColor.getBlue();
		  for (int i = 0; i < drawLength; i++) {  //循环画出每一帧！！  
	          int li_r =  baseColor.getRed()   +i*s; //R  
	          int li_g =  baseColor.getGreen() +i*s; //G  
	          int li_b =  baseColor.getBlue()  +i*s; //B  
	          int li_a =  baseColor.getAlpha() -i*s; //A
	          //如果溢出255,则当255,否则报错!  
	          if (li_r > p_r) {  
	              li_r = p_r;  
	          }  
	          if (li_g > p_g) {  
	              li_g = p_g;  
	          }  
	          if (li_b > p_b) {  
	              li_b = p_b;  
	          }  
	          if (li_a < 0){
	        	  li_a = 0;
	          }
	          Color  color = new Color(li_r,li_g,li_b,li_a);
	          g.setColor( color); //设置颜色!! 
	          int x = drawLength - i  ; //计算绘制坐标
	          int y = drawLength - i  ;
	          RoundRectangle2D roundedRectangle  = null;
	          if(isInner){
	        	  roundedRectangle  = new RoundRectangle2D.Float(i,i,width -2*i, height -2*i , range, range);
	          }else{
	        	  roundedRectangle = new RoundRectangle2D.Float(x,y,width -2*x, height -2*y , range, range);
	          }
	          ((Graphics2D) g).draw(roundedRectangle);
//	          RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(x,y,width -2*x, height -2*y , range, range);
	      }  
	  }
	 
	 
	 
	 
	  public static void main(String[] args) {
		   //水晶特效按钮 +阴影面板
		   ShadowPanel shadowpane = new ShadowPanel( new CrystalButton(new Color(3, 26, 61), "颜色按钮", 150, 20),Color.gray, 20 ,20);
		   ShadowPanel shadowpane1 = new ShadowPanel( new CrystalButton(new Color(76, 93, 142), "颜色按钮2", 150, 20),Color.gray, 20 ,20);
		   ShadowPanel shadowpane2 = new ShadowPanel( new CrystalButton( new Color(144, 29, 67), "颜色按钮3", 150, 20),Color.gray, 20 ,20);
		   ShadowPanel shadowpane3 = new ShadowPanel( new CrystalButton(new Color(158, 84, 167), "颜色按钮4", 150, 20),Color.gray, 20 ,20);
		    shadowpane.setPreferredSize(new Dimension(300, 80));
		    shadowpane1.setPreferredSize(new Dimension(300, 80));
		    shadowpane2.setPreferredSize(new Dimension(300, 80));
		    shadowpane3.setPreferredSize(new Dimension(300, 80));
			JFrame frame = new JFrame();
			frame.setLayout(new FlowLayout());
		    frame.add(shadowpane);
		    frame.add(shadowpane1);
		    frame.add(shadowpane2);
		    frame.add(shadowpane3); 
		    JFrameUtils.setDefaultWindow(frame, 600, 600);
		}
}
